package com.haixiaoke.saas.system.controller.system;


import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.constant.Constants;
import com.haixiaoke.saas.common.constant.PlatformConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.domain.entity.SysMenu;
import com.haixiaoke.saas.common.core.domain.entity.SysUser;
import com.haixiaoke.saas.system.domain.LoginBody;
import com.haixiaoke.saas.system.domain.LoginUser;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import com.haixiaoke.saas.system.framework.web.service.SysLoginService;
import com.haixiaoke.saas.system.framework.web.service.SysPermissionService;
import com.haixiaoke.saas.system.framework.web.service.TokenService;
import com.haixiaoke.saas.system.service.ISysMenuService;
import com.haixiaoke.saas.user.domain.UserBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author haixiaoke
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/enterprise/login")
    public AjaxResult enterpriseLogin(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        LoginUser loginUser = loginService.enterpriseLogin(loginBody.getPhonenumber(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        loginUser.setPlatform(PlatformConstants.PC_BUSINESS);
        String token = tokenService.createToken(loginUser);
        ajax.put(Constants.TOKEN, token);
        ajax.put(Constants.TENANTID, loginUser.getTenantId());
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getPhonenumber(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录方法-微信一键登录
     */
    @PostMapping("/weChat/login")
    public AjaxResult weChatLogin(@RequestBody LoginBody loginBody) {
        LoginUser loginUser = loginService.weChatSelectUserByPhone(loginBody);
        loginUser.setPlatform(PlatformConstants.MP_WECHAT_BUSINESS);
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = tokenService.createToken(loginUser);
        ajax.put(Constants.TOKEN, token);
        ajax.put(Constants.TENANTID, loginUser.getTenantId());
        return ajax;
    }

    /**
     * 获取企业端用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/enterprise/getInfo")
    public AjaxResult enterpriseGetInfo() {
        UserBusiness user = SecurityUtils.getLoginUser().getBizUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user.getUserId(), SecurityUtils.getLoginUser().getTenantId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user, SecurityUtils.getLoginUser().getTenantId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("/enterprise/getRouters")
    public AjaxResult enterpriseGetRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByBizUserId(userId, SecurityUtils.getLoginUser().getTenantId(),
                SecurityUtils.getLoginUser().getBizUser().getRoleIds());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
